package com.onedec.materials.controller;

import com.onedec.materials.client.ProjectClient;
import com.onedec.materials.entity.OnedecMaterialList;
import com.onedec.materials.entity.OnedecReceipt;
import com.onedec.materials.mapper.OnedecMaterialListMapper;
import com.onedec.materials.service.FileService;
import com.onedec.materials.service.MaterialListService;
import com.onedec.materials.service.ReceiptService;
import com.onedec.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 添加材料
 */
@RestController
@Log4j2
@Api(tags = "图片上传接口")
public class MaterialListController {
    @Autowired
    private MaterialListService materialListService;
    /**
     * 添加订单
     */
    @Autowired
    private ProjectClient projectClient;
    @Autowired
    private FileService fileService;
    @Value("${baseUploadUrl}")
    private String url;

    @PostMapping("addMaterialList")
    @ApiOperation(value = "单个图片上传到七牛云")
    public ResponseEntity addMaterialList(@RequestBody List<OnedecMaterialList> onedecMaterialLists) {
        /*@RequestBody List<OnedecMaterialList> onedecMaterialLists*/
        log.info("打印出来的数据:{}" + onedecMaterialLists);
        int i = materialListService.addMaterialLists(onedecMaterialLists);
        Integer status = 0;
        if (onedecMaterialLists.get(0).getOperationNum() == 1) {
            status = 8;
        } else if (onedecMaterialLists.get(0).getOperationNum() == 2) {
            status = 12;
        } else if (onedecMaterialLists.get(0).getOperationNum() == 3) {
            status = 16;
        } else if (onedecMaterialLists.get(0).getOperationNum() == 4) {
            status = 20;
        } else if (onedecMaterialLists.get(0).getOperationNum() == 5) {
            status = 24;
        }

        if (i > 0) {
            projectClient.update(0, onedecMaterialLists.get(0).getMaterialListBak1(), status);
            return new ResponseEntity("添加成功", "200");
        }
        return new ResponseEntity("失败", "400");
    }

    @GetMapping("selectMateriaListByProject")
    public ResponseEntity addMaterialList(String projectNo, Integer inPhase) {
        final List<Map<String, Object>> maps = materialListService.selectMaterialListByProject(projectNo, inPhase);
        return new ResponseEntity(maps);
    }

    @Resource
    private ReceiptService receiptService;

    @GetMapping("receipt")
    public ResponseEntity selectReceipt(String projectNo, Integer inPhase) {
        final List<OnedecReceipt> receipts = receiptService.selectReceipt(projectNo, inPhase);
        return new ResponseEntity(receipts);
    }

    @PostMapping("onedecmaterialsUpload/{projectNo}/{inPhase}")
    public ResponseEntity onedecmaterialsUpload(@PathVariable("projectNo") String projectNo, @PathVariable("inPhase") Integer inPhase, MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        String fileName = file.getOriginalFilename();
        File file1 = new File(url + fileName);//创建本地文件
        try {
            //将MulitpartFile文件转化为file文件格式
            file.transferTo(file1);
            Map response = fileService.uploadFile(file1);
            Object imageName = response.get("imgName");
            System.out.println("qddyllf7e.bkt.clouddn.com" + imageName);
            final OnedecReceipt receipt = new OnedecReceipt();
            receipt.setReceiptOperationNum(inPhase);
            receipt.setReceiptReceipt1("http://qddyllf7e.bkt.clouddn.com/" + imageName);
            receipt.setReceiptReceipt2(projectNo);
            receiptService.insertReceipt(receipt);
            final Map<String, Object> re = new HashMap<>();
            re.put("src", "http://qddyllf7e.bkt.clouddn.com/" + imageName);
            final ResponseEntity<Map<String, Object>> responseEntity = new ResponseEntity<>(re);
            responseEntity.setCode("0");
            return responseEntity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ERROR;
    }

}
